package eco.lamp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import eco.lamp.bl.LampUpgradeItem;

public class LampsListActivity extends Activity {

	ArrayList<LampUpgradeItem> mItems = new ArrayList<LampUpgradeItem>();
	ListView lvLamps;
	LampsUpgradeAdaptor mAdapter;
	final static int REQUEST_UPGRADE = 1;
	final static int REQUEST_SUMMARY = 3;
	Button mBntExit;
	Button mBtnNext;
	ImageButton mBtnSettings;
	Button mBtnAdd;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.lamp_list);
		lvLamps = (ListView) findViewById(R.id.lvLamps);
		mAdapter = new LampsUpgradeAdaptor();
		lvLamps.setAdapter(mAdapter);

		mBtnAdd = (Button) findViewById(R.id.btnAdd);

		mBtnAdd.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				showUpgrade(view);
			}

		});

		mBtnNext = (Button) findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				showSummary(view);
			}
		});

		mBntExit = (Button) findViewById(R.id.btnListExit);
		mBntExit.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				finish();
			}
		});
		
		mBtnNext = (Button) findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				showSummary(view);
			}
		});
		
		mBtnSettings = (ImageButton)findViewById(R.id.btnSettings);
		mBtnSettings.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// Launch Preference activity
				Intent i = new Intent(LampsListActivity.this, Preferences.class);
				startActivity(i);	
			}
		});

		// showUpgrade(this.mBtnAdd);
		updateButtons();
	}

	private void showUpgrade(View view) {
		Intent myIntent = new Intent(view.getContext(),
				LampUpgradeActivity.class);
		startActivityForResult(myIntent, REQUEST_UPGRADE);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelableArrayList("LampUpgradeItem", mItems);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mItems = savedInstanceState.getParcelableArrayList("LampUpgradeItem");
		updateButtons();
	}

	// Listen for results.
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// See which child activity is calling us back.
		switch (requestCode) {
		case REQUEST_UPGRADE:
			if (resultCode == RESULT_OK) {
				Bundle result = data.getExtras();
				LampUpgradeItem item = result.getParcelable("LampUpgradeItem");
				mItems.add(item);
				mAdapter.notifyDataSetChanged();
				updateButtons();
			} else if (resultCode == RESULT_CANCELED) {
				if (mItems.size() == 0) {
					finish();
				}
			}
			break;
		case REQUEST_SUMMARY:
			if (resultCode == RESULT_OK)
				finish();
			break;
		default:
			break;
		}
	}

	private void updateButtons() {
		boolean itemsAv = mItems.size() > 0;
		mBntExit.setVisibility(itemsAv ? View.GONE : View.VISIBLE);
		mBtnNext.setVisibility(itemsAv ? View.VISIBLE : View.GONE);
	}

	private void showSummary(View view) {
		Bundle data = new Bundle();
		data.putParcelableArrayList("LampUpgradeItem", mItems);
		Intent myIntent = new Intent(view.getContext(), SummaryActivity.class);
		myIntent.putExtras(data);
		startActivityForResult(myIntent, REQUEST_SUMMARY);
	}

	private class LampsUpgradeAdaptor extends BaseAdapter {
		public int getCount() {
			return mItems.size();
		}

		public Object getItem(int location) {
			return mItems.get(location);
		}

		public long getItemId(int location) {
			return location;
		}

		// disabling list items select
		public boolean areAllItemsEnabled() {
			return false;
		}

		public boolean isEnabled(int position) {
			return false;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.lamp_row, null);
			}

			TextView txtFrom = (TextView) v.findViewById(R.id.txtLampRowFrom);
			TextView txtTo = (TextView) v.findViewById(R.id.txtLampRowTo);
			ImageButton btnDelete = ((ImageButton) v
					.findViewById(R.id.btnRowDelete));
			final LampUpgradeItem item = mItems.get(position);
			
			
			txtFrom.setText(item.mLampOld.toString(getResources()));
			txtTo.setText(item.mLampNew.toString(getResources()));

			btnDelete.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					mItems.remove(item);
					mAdapter.notifyDataSetChanged();
					lvLamps.requestLayout();
					updateButtons();
				}
			});
			return v;
		}
	}
}
